package com.hp.controller;

import com.hp.annotation.Power;
import com.hp.entity.Menus;
import com.hp.entity.User;
import com.hp.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.*;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;

@Power(name = "主页面", type = 0)
@Controller
public class MainController {
    @Autowired
    private UserService userService;

    @Power(name = "去主页面", type = 1, url = "/main")
    @GetMapping("/main")
    public String main() {

        return "main";
    }

    @Power(name = "去欢迎页面", type = 1, url = "/welcome")
    @GetMapping("/welcome")
    public String welcome() {

        return "welcome";
    }

    @Power(name = "登入", type = 1, url = "/dologin", isPublic = true)
    @PostMapping("/dologin")
    public void dologin(User user, HttpSession session, HttpServletRequest request, HttpServletResponse response) throws Exception {
        userService.dologin(user, session, request, response);
    }

    @ResponseBody
    @Power(name = "生成菜单", type = 1, url = "/menu")
    @GetMapping("/menu")
    public Menus menu(HttpSession session) {
        List<Menus> menus = (List<Menus>) session.getAttribute("menusList");
        List<Menus> menuslist = new ArrayList<>();
        Menus mm=new Menus();
        Menus parent = null;
        //生成结构竖
        for (Menus menu : menus) {//找父类
            if (menu.getParentid() == null || menu.getParentid() == 0) {
                parent = menu;
                menus.remove(menu);
                break;
            }
        }

        if (parent == null) {//有父类
            parent = new Menus();
            parent.setIcon("icon-sys");
            parent.setMenuid("1");
            parent.setMenuname("系统设置");
        }

        parent.setMenus(menus);//给子树
        menuslist.add(parent);
        mm.getMenus().add(parent);
        return mm;
    }


    public static void main(String[] args) throws Exception {
        int a=30;
        int b=50;
        System.out.println("a:"+a);
        System.out.println("b:"+b);
        a=a+b;      //30+50=80      a=80
        b=a-b;      //80-50=30      b=30
        a=a-b;      //80-30=50      a=50
        System.out.println("a:"+a);
        System.out.println("b:"+b);
    }




}





